package com.type.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.type.service.TypeFourService;

@Controller
public class TypeFourController {
	
	@Resource(name = "TypeFourService")
	TypeFourService service;
	
/*	@RequestMapping("/type4/goAddrSearch.do")
	public String searchAddr(HttpServletRequest req, Model model,@RequestParam(value="str") String str) {
		System.out.println(str);
		ArrayList list = new ArrayList();
		if(str.length()!=0){
			System.out.println(str.length());
			try {
				list = service.getAddress(str);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			model.addAttribute("list", list);
		} else {
			
		}
//		System.out.println("hi");
		return "/type/typeFour/searchAddr2";
	}*/
		
	@RequestMapping("/type4/addTab.do")
	public String addTab() {
		return "/type/typeFour/cForm";
	}
	
	@RequestMapping("/type4/insProduct.do")
	public String insertProduct(HttpServletRequest req, Model model) {
		Enumeration enu = req.getParameterNames();
		HashMap<String, String> map = new HashMap<String, String>();
		while (enu.hasMoreElements()) {
		   String key = (String)enu.nextElement() ;
		   String value = req.getParameter(key);
		   map.put(key, value);
		   System.out.println(key +":"+value);
		}
		try {
			service.insertProduct(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			String msg = e.getMessage();
			model.addAttribute("msg", msg);
		}

		return "/type/typeFour/result";
	}
	
	@RequestMapping("/type4/insCorp.do")
	public String insertCorperation(HttpServletRequest req, Model model) {
		Enumeration enu = req.getParameterNames();
		HashMap<String, String> map = new HashMap<String, String>();
		while (enu.hasMoreElements()) {
		   String key = (String)enu.nextElement() ;
		   String value = req.getParameter(key);
		   map.put(key, value);
		   System.out.println(key +":"+value);
		}
		
		try {
			service.insertCorperation(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			String msg = e.getMessage();
			model.addAttribute("msg", msg);
		}
		
		return "/type/typeFour/result";
	}
	
	@RequestMapping(value="/type4/getCList.do")
	public String getCorpList(Model m){
		ArrayList list = service.getCList();
		m.addAttribute("cList", list);
		
		return "/type/typeFour/clist";	
	}
	
}
